package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.SecretChoosedUser;
import com.cyanite.playlv2api.types.User;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

public class SecretFriendListAdapter  extends BaseGroupAdapter<User> implements ObservableAdapter{

    private LayoutInflater mInflater;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();
    private ButtonRowClickHandler mClickListener;
    private Vector<SecretChoosedUser> mChoosedFrirnds;

    public SecretFriendListAdapter(Context context, RemoteResourceManager rrm, ButtonRowClickHandler clickListener) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mRrm = rrm;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mRrm.addObserver(mResourcesObserver);
        mClickListener = clickListener;
    }

    public String getUsersIdString(){
        String separator = "{p}";
        String userIdString=new String();
        for(SecretChoosedUser it : mChoosedFrirnds){
            if(it.getIsChoosed() == true){
                userIdString+=it.getUserId()+separator;
            }
        }
        return userIdString;
    }
    
    private void setAllChooseStatue(boolean isTrue){
        for(SecretChoosedUser it : mChoosedFrirnds){
            it.setIsChoosed(isTrue);
            }
    }
    
    public void setIsAllFriends(boolean isTrue){
        setAllChooseStatue(isTrue);
    }
    
    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhotos);
        mRrm.deleteObserver(mResourcesObserver);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.secret_friends_list_item, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.ivSecretUserPhoto);
            holder.name = (TextView) convertView.findViewById(R.id.tvSecretUserName);
            holder.clickable = (LinearLayout) convertView.findViewById(R.id.llSecretListItemClickableArea);
            holder.choose = (ImageView) convertView.findViewById(R.id.ivSecretRight);
            holder.choose.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    int index = (Integer) v.getTag();
                    SecretChoosedUser scUser = mChoosedFrirnds.get(index);
                    if(scUser.getIsChoosed()){
                        scUser.setIsChoosed(false);
                        holder.choose.setImageResource(R.drawable.checkbox_off);
                    }else{
                        scUser.setIsChoosed(true);
                        holder.choose.setImageResource(R.drawable.checkbox_on);
                    }
                    mClickListener.onCheckButtonClick(scUser.getIsChoosed());
                }
            });

            holder.clickable.setOnClickListener(mOnClickListenerInfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        User user = (User) getItem(position);
        holder.name.setText(user.getUserName());

        if (user.getPhoto() != null && !user.getPhoto().equals(""))    {
            final Uri photoUri = Uri.parse(user.getPhoto());
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                holder.photo.setImageBitmap(bitmap);
            } catch (IOException e) {
                holder.photo.setImageResource(R.drawable.trip_default_photo);
            }
        }   else {
            holder.photo.setImageResource(R.drawable.trip_default_photo);
        }
        SecretChoosedUser scUser = mChoosedFrirnds.get(position);

        if(scUser.getIsChoosed()){
            holder.choose.setImageResource(R.drawable.checkbox_on);
        }else{
            holder.choose.setImageResource(R.drawable.checkbox_off);
        }
        holder.clickable.setTag(position);
        holder.choose.setTag(position);
        return convertView;
    }

    @Override
    public void setGroup(Group<User> g) {
        super.setGroup(g);

        mChoosedFrirnds = new  Vector<SecretChoosedUser>();

        for (User it : g) {
            SecretChoosedUser scUser = new SecretChoosedUser();
            scUser.setUserId(it.getId());
            mChoosedFrirnds.add(scUser);
            if (it.getPhoto() != null && !it.equals(""))  {
                Uri photoUri = Uri.parse(it.getPhoto());
                if (!mRrm.exists(photoUri)) {
                    mRrm.request(photoUri);
                }
            }
        }
    }

    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhotos);
        }
    }

    private Runnable mUpdatePhotos = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };
    
    private OnClickListener mOnClickListenerInfo = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position = (Integer) v.getTag();
            mClickListener.onClickInfo((User) getItem(position));
        }
    };

    public interface ButtonRowClickHandler {
        public void onCheckButtonClick(boolean isTrue);
        public void onClickInfo(User user);
    }

    private static class ViewHolder {
        LinearLayout clickable;
        ImageView photo;
        TextView name;
        ImageView choose;
    }
}
